package com.bac.rentmap.web.controller.osmimport;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.bac.rentmap.manager.BuildingManager;
import com.bac.rentmap.model.Building;
import com.bac.rentmap.osm.BuildingsReader;

/**
 *
 * @author Ivan Holub
 */
public class AjaxImportBuildingsController extends AbstractController {

    private BuildingManager buildingManager;

    private BuildingsReader buildingsReader;

    public void setBuildingManager(BuildingManager buildingManager) {
        this.buildingManager = buildingManager;
    }

    public void setBuildingsReader(BuildingsReader buildingsReader) {
        this.buildingsReader = buildingsReader;
    }

    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        int maxNumber = buildingManager.getMaxNumber("minsk");
        List<Building> buildings = buildingsReader.readBuildings(getServletContext(), httpServletRequest.getParameter("city"));
        int count = buildings.size();
        ModelAndView mav = new ModelAndView("admin/import/osm/ajaximportbuildings");
        mav.addObject("count", count);
        mav.addObject("maxNumber", maxNumber);
        return mav;
    }
}